package com.caipiao.controller;

import com.alibaba.fastjson.JSONObject;
import com.caipiao.mapper.SBankMapper;
import com.caipiao.pojo.ObjectResult;
import com.caipiao.pojo.SBank;
import com.caipiao.pojo.XMLBean;
import com.caipiao.service.UserBankService;
import com.caipiao.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/1/6.
 */
@Controller
@RequestMapping("/bank")
public class UserBankController {

    @Autowired
    private UserBankService userBankService;
    @Autowired
    private SBankMapper sBankMapper;

    @ResponseBody
    @RequestMapping(value = "/bindingBankNo",method = RequestMethod.POST,produces = "application/json")
    public ObjectResult bindingBankNo(XMLBean xmlBean) throws UnsupportedEncodingException {
        if (StringUtil.isEmpty(xmlBean.getBankcard()) || StringUtil.isEmpty(xmlBean.getIdcard())
                || StringUtil.isEmpty(xmlBean.getRealname()) || StringUtil.isEmpty(xmlBean.getMobile())){
            return ObjectResult.build("0", "参数丢失,请重试");
        }

        if (xmlBean.getBankcard().length() < 16 || xmlBean.getBankcard().length() > 21){

            return ObjectResult.build("0", "银行卡位数错误");

        }else if (!BankCardUtils.checkBankCard(xmlBean.getBankcard())){

            return ObjectResult.build("0", "银行卡错误");

        } else if (!IDCardUtils.cardCodeVerifySimple(xmlBean.getIdcard())){

            return ObjectResult.build("0", "身份证位数错误");

        }else if (!IDCardUtils.cardCodeVerify(xmlBean.getIdcard())){

            return ObjectResult.build("0", "身份证错误");

        } else if (xmlBean.getMobile().length() != 11){

            return ObjectResult.build("0", "手机号位数错误");

        }else if (!xmlBean.getMobile().substring(0,1).equals("1") || xmlBean.getMobile().substring(0,2).equals("14")){

            return ObjectResult.build("0", "手机号错误");
        }

        return userBankService.bindingBankNo(xmlBean);
    }


    /**
     * 解绑银行卡
     * @param xmlBean
     * @return
     */
    @RequestMapping("relieveBank")
    @ResponseBody
    public ObjectResult relieve_bank_no(XMLBean xmlBean) {
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }

        return userBankService.relieveBank(xmlBean);
    }


    /**
     * 银行卡列表
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/bankList")
    @ResponseBody
    public ObjectResult bankList(XMLBean xmlBean){
        List<SBank> banks = sBankMapper.selectBankList();
        for (SBank sBank:banks){
            sBank.setLogo(ConfigUtils.ADMIN_URL+sBank.getLogo());
        }

        return ObjectResult.build("1","成功",banks);
    }

    /**
     * 用户银行卡
     * @param xmlBean
     * @return
     */
    @RequestMapping(value = "/userBank")
    @ResponseBody
    public ObjectResult userBank(XMLBean xmlBean){
        if(StringUtil.isEmpty(xmlBean.getToken())){
            return ObjectResult.build("0","未登录");
        }

       return userBankService.userBank(xmlBean);
    }


}
